package com.commerceerp.system.util;

import com.ebay.sdk.ApiCall;
import com.ebay.soap.eBLBaseComponents.GetItemRequestType;
import com.ebay.soap.eBLBaseComponents.GetOrdersResponseType;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 时间工具
 * Created by Administrator on 2018/3/26.
 */
public class DateUtil {

    //当前日期加上day天
    public static String getAfterDate(String date,int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date d=format.parse(date);
            Date date2 = new Date(d.getTime() + day * 24 * 60 * 60 * 1000);
            return format.format(date2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    //为获取当前系统时间
    public static String getSystemTime(String pattern) {
        SimpleDateFormat df = new SimpleDateFormat(pattern);//设置日期格式
        String str = df.format(new Date());// new Date()为获取当前系统时间
        return str;
    }

    /**
     * 求两个时间相差的天、时、分
     * @param startTime
     * @param endTime
     * @param format
     * @param n 0.天 1.时 2.分
     * @return
     */
    public static long dateDiff(String startTime, String endTime, String format,int n) {
        //按照传入的格式生成一个simpledateformate对象
        SimpleDateFormat sd = new SimpleDateFormat(format);
        long nd = 1000 * 24 * 60 * 60;//一天的毫秒数
        long nh = 1000 * 60 * 60;//一小时的毫秒数
        long nm = 1000 * 60;//一分钟的毫秒数
        long diff = 0;
        try{
            //获得两个时间的毫秒时间差异
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();
        }catch (Exception e){

        }
        long day = diff / nd;//计算差多少天
        long hour = diff / nh;//计算差多少小时
        long min = diff / nm;//计算差多少分钟
        long time[] = {day,hour,min};
        //System.out.println("时间相差：" + day + "天" + hour + "小时" + min + "分钟" );
        return time[n];
    }

    //比较两个时间
    public static boolean compareDate(String startTime, String endTime){
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date1 = sf.parse(startTime);
            Date date2 = sf.parse(endTime);
            return !date1.after(date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 时间默认值
     */
    public static Map<String,Object> getDefaultDate(String sDate, String eDate){

        if((sDate == null && eDate != null) || "".equals(sDate) && !"".equals(eDate)){
            sDate = DateUtil.getDateBeforeFifteen(eDate);
        }else if(sDate != null && eDate == null || "".equals(eDate) && !"".equals(sDate)){
            eDate = DateUtil.getDateLaterFifteen(sDate);
        }else if(sDate == null && eDate == null || "".equals(sDate) ||"".equals(eDate)){
            eDate = DateUtil.getYMDDate();
            sDate = DateUtil.getDateBeforeFifteen(eDate);
        }
        Map<String,Object> Date = new HashMap<String,Object>();
        Date.put("sDate",sDate);
        Date.put("eDate",eDate);
        return Date;
    }

    /**
     * 将某个日期往前推15天
     * @param Date
     * @return
     */
    public static String getDateBeforeFifteen(String Date){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf1.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar   calendar   =   new   GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,-14);//把日期往后增加8天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往前推8天的结果
        String DATE = sdf1.format(date);
        return DATE;
    }

    /**
     * 将某个日期往后推15天
     * @param Date
     * @return
     */
    public static String getDateLaterFifteen(String Date){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf1.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar   calendar   =   new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE,14);//把日期往后增加8天.整数往后推,负数往前移动
        date=calendar.getTime();   //这个时间就是日期往后推8天的结果
        String DATE = sdf1.format(date);
        return DATE;
    }

    /**
     * 获得该月最后一天
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year,int month){
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /**
     * 字符串时间转Date类型
     */
    public static Date getStringToDateTime(String strDate){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return sdf.parse(strDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 字符串时间转Calendar
     */
    public static Calendar getCalendarToStringTime(String strTime){

        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");

        Date date = null;
        try {
            date = sdf.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar;
    }

    //获取YYYY-MM-DD HH:mm:ss的当前时间
    public static String getYMDHMSDate() {
        Date Date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(Date).trim();
    }

    //获取yyyy-MM-dd格式的当前时间
    public static String getYMDDate() {
        Date Date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(Date);
    }

    //获取yyyy-MM-dd格式的当前时间
    public static String getHMSDate() {
        Date Date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        return sdf.format(Date);
    }

    /**
     * 获取当前时间id
     */
    public static String getTimeId(){

            String id="";
            //获取当前时间戳
            SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
            String temp = sf.format(new Date());
            //获取6位随机数
            int random=(int) ((Math.random()+1)*100);
            id=temp+random;
            return id;

    }


    /**
     * 将某个日期往前推6天
     *
     * @param Date
     * @return
     */
    public static String getDateBeforeSixDay(String Date) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf1.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -5);//把日期往后增加6天.整数往后推,负数往前移动
        date = calendar.getTime();   //这个时间就是日期往前推6天的结果
        String DATE = sdf1.format(date);
        return DATE;
    }

    /**
     * 获取某个日期之前的7天(集合)
     */
    public static List<String> getBeforeBetweenSevenDay(String Date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(Date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar calendar1 = new GregorianCalendar();
        Calendar calendar2 = new GregorianCalendar();
        calendar1.setTime(date);
        calendar2.setTime(date);
        calendar2.add(calendar2.DATE, -5);//把日期往后增加8天.整数往后推,负数往前移动
        List<String> Dates = new ArrayList<String>();
        while (calendar2.getTime().before(calendar1.getTime()) || calendar2.getTime().equals(calendar1.getTime())) {//判断是否到结束日期
            String str = sdf.format(calendar2.getTime());
            Dates.add(str);
            calendar2.add(Calendar.DAY_OF_YEAR, 1);//进行当前日期加1

        }
        return Dates;
    }


}
